package cn.lxs.web.servletContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@WebServlet("/ContextDemo5")
public class ContextDemo5 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        /*获取文件真实路径*/
        String realPath = context.getRealPath("/b.txt");
        // '/'代表的是 在web目录下访问
        System.out.println(realPath);
        //打印一下,这是web目录下的文件

        String realPath1 = context.getRealPath("/WEB-INF/c.txt");
        System.out.println(realPath1);
        //获取c文件,WEB-INF目录下的文件

        String realPath2 = context.getRealPath("/WEB-INF/classes/a.txt");
        System.out.println(realPath2);
        //src目录下的文件
    }
}
